// circle/square/square.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schoolList: [{
      img: 'https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3977491455,761565125&fm=58',
      name: '浙江大学'
    }],
    shenghuolList: [],
    yishuList: [],
    meishiList: [],
    jianshenList: [],
    lvyouList: [],
    imgonepath: ''
  },
  creatcircle: function() {
    wx.navigateTo({
      url: '../creatcircle/creatcircle'
    })
  },
  getCircleDAta: function(type) {
    var datas = [];
    var imgarr = [];
    var types = (type ? type : '');
    var db = wx.cloud.database();
    var that = this;
    console.log(types);
    var data = db.collection('circle').where({
      // type: types.toString()
    }).get({
      success: function(res) {
        var schoolList = [],
          shenghuolList = [],
          yishuList = [],
          meishiList = [],
          jianshenList = [],
          lvyouList = [];
        datas = res.data;
        if (datas.length) {
          var oneimg = datas[0]['qzimgID'];
          console.log(oneimg);
          wx.cloud.getTempFileURL({
            fileList: [{
              fileID: oneimg,
              maxAge: 60 * 60,
            }]
          }).then(res => {
            var imgold = res.fileList[0].tempFileURL;
            console.log(imgold);
            var index = imgold.lastIndexOf("\/");
            var imgBefor = imgold.substring(0, index + 1);
            console.log(imgBefor);
            for (var i = 0; i < datas.length; i++) {
              var imgid = datas[i]['qzimgID'];
              var imgAfter = imgid.substring(imgid.lastIndexOf("/") + 1);
              datas[i]['img'] = imgBefor + imgAfter;
              if (datas[i]['type'] == 10) {
                schoolList.push(datas[i]);
              } else if (datas[i]['type'] == 0) {
                yishuList.push(datas[i]);
              } else if (datas[i]['type'] == 1) {
                meishiList.push(datas[i]);
              } else if (datas[i]['type'] == 2) {
                jianshenList.push(datas[i]);
              } else if (datas[i]['type'] == 3) {
                lvyouList.push(datas[i]);
              } else if (datas[i]['type'] == 4) {
                shenghuolList.push(datas[i]);
              }
              that.setData({
                schoolList: schoolList,
                shenghuolList: shenghuolList,
                yishuList: yishuList,
                meishiList: meishiList,
                jianshenList: jianshenList,
                lvyouList: lvyouList
              })
              wx.stopPullDownRefresh();
            }
          }).catch(error => {})
          // console.log(oneimg);
        }
      }
    })
  },
  // 查看更多
  viewMore: function() {
    wx.navigateTo({
      url: '../viewMore/viewMore',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(1)
    this.getCircleDAta(0)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // this.getCircleDAta(0)
    // this.getCircleDAta(1)
    // this.getCircleDAta(2)
    // this.getCircleDAta(3)
    // this.getCircleDAta(4)
    // this.getCircleDAta(10)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getCircleDAta(0)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})